<?php
/**
 * Created by PhpStorm.
 * User: Hongjw
 * Date: 2018/3/17
 * Time: 10:44
 */

namespace App\Http\Controllers\Laravel;


use App\BCAM\CMPlaceHolder\CMPlaceHolderType;
use App\Http\Controllers\Controller;
use App\Model\Cgs\Placeholder;
use App\Tools\Math\CmdHelper;
use App\Tools\Math\RPNotation;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Log;
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\PhpWordTest;
use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\Shared\Html;
use Yajra\DataTables\Facades\DataTables;

class UEController extends Controller
{
    public function showBaseView(){
        //return DataTables::of($placeholder->query())->make();
        $placeholderType = getEnumArr('placeHolderType');
        return view("admin.ue_test");
    }

    public function postDataForPlaceholderList(Request $request,Placeholder $placeholder){
        Log::info(var_export($request->all(),true));
        //$placeholderList = $placeholder->get();
        return DataTables::of($placeholder->query())->make();
        /*return $placeholderList->map(function ($item, $key) {
            return $item ->toArray();
        })->toArray();*/
        //dump();
    }

    public function postContent(Request $request,Placeholder $placeholder){
        $content = $request->get("content");
        preg_match_all("/{{(.*?)}}/",$content,$placeHolderNameArr);
        if(!empty($placeHolderNameArr)){
            $placeHolderCodeArr = $placeHolderNameArr[1];
            $placeholderF = $placeholder->select('id')->whereIn('placeholder_code',$placeHolderCodeArr)->count();
            if($placeholderF != count($placeHolderCodeArr)){
                //return "fail";
            }
            /*foreach ($placeHolderNameArr[1] as $key => $value){
                $placeholderF = $placeholder->select('id')->where('placeholder_code',$value)->first();
                dump($placeholderF,$value);
                if(is_null($placeholderF)){
                    return "123";
                }
            }*/
        }
        $phpWord = new PhpWord();
        $section = $phpWord->addSection();
        Html::addHtml($section, $content);
        $xmlWriter = IOFactory::createWriter($phpWord, 'Word2007');
        $xmlWriter->save(storage_path('word/中文名如何.docx'));
        $rendererName = Settings::PDF_RENDERER_TCPDF;
        $rendererLibraryPath = realpath(base_path() . '/vendor/tecnickcom/tcpdf');
        Settings::setPdfRenderer($rendererName, $rendererLibraryPath);
        $xmlWriter2 = IOFactory::createWriter($phpWord, 'PDF');
        $xmlWriter2->setFont('stsongstdlight')->save(storage_path('123.pdf'));

        echo "success";
        //保存
    }

    public function testRPNotation(){
        /*$res = RPNotation::calculate("80000/(C*(A+B)+G*A)/ A * B*100/3",["A" => 2,"B" => 4,"C" => 5, "G" => 5]);
        echo CmdHelper::numToCnyUnUnit(34342343332423.856);*/
        $phpWord = new \PhpOffice\PhpWord\PhpWord();
        /* Note: any element you append to a document must reside inside of a Section. */

// Adding an empty Section to the document...
        $section = $phpWord->addSection();
// Adding Text element to the Section having font styled by default...
        $section->addText(
            '"Learn from yesterday, live for today, hope for tomorrow. '
            . 'The important thing is not to stop questioning." '
            . '(Albert Einstein) 中文'
        );

        /*
         * Note: it's possible to customize font style of the Text element you add in three ways:
         * - inline;
         * - using named font style (new font style object will be implicitly created);
         * - using explicitly created font style object.
         */

// Adding Text element with font customized inline...
        $section->addText(
            '"Great achievement is usually born of great sacrifice, '
            . 'and is never the result of selfishness." '
            . '(Napoleon Hill)',
            array('name' => 'Tahoma', 'size' => 10)
        );

// Adding Text element with font customized using named font style...
        $fontStyleName = 'oneUserDefinedStyle';
        $phpWord->addFontStyle(
            $fontStyleName,
            array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true)
        );
        $section->addText(
            '"The greatest accomplishment is not in never falling, '
            . 'but in rising again after you fall." '
            . '(Vince Lombardi)',
            $fontStyleName
        );

// Adding Text element with font customized using explicitly created font style object...
        $fontStyle = new \PhpOffice\PhpWord\Style\Font();
        $fontStyle->setBold(true);
        $fontStyle->setName('Tahoma');
        $fontStyle->setSize(13);
        $myTextElement = $section->addText('"Believe you can and you\'re halfway there." (Theodor Roosevelt)');
        $myTextElement->setFontStyle($fontStyle);

// Saving the document as OOXML file...
        $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
        $objWriter->save('helloWorld.docx');

// Saving the document as ODF file...
        $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText');
        $objWriter->save('helloWorld.odt');

// Saving the document as HTML file...
        $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
        $objWriter->save('helloWorld.html');

        /* Note: we skip RTF, because it's not XML-based and requires a different example. */
        /* Note: we skip PDF, because "HTML-to-PDF" approach is used to create PDF documents. */
        echo "end";
    }

}